package com.mapproject.industrymap.controller;

import com.mapproject.industrymap.utils.ReturnValue;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTStyleManager;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.net.URLDecoder;
import java.util.List;

@RestController
@RequestMapping("/onlineStyler")
public class OnlineStylerController {

    @PostMapping(value = "/styler1")
    public ReturnValue getNewsmsg1(@RequestBody String sldFile) {
        System.out.println(sldFile);
        return new ReturnValue(200,"sssss");
    }
    /**
     * 发布wms服务参数为待发布图层表名
     * @param sldFile
     * @return
     */
    @PostMapping(value = "/styler")
    public ReturnValue getNewsmsg(@RequestBody Object sldFile, @RequestParam(value = "tablename")String table_name, @RequestParam(value = "sldname")String sldName) {
        //geoserver连接配置
        String RESTURL  = "http://localhost:8080/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        try {
            //服务参数
            String ws = "WJMap";
            String store_name = "WJMap";
            String sldContentStr = "";

            if(sldFile == null || sldFile.toString().isEmpty()){
                System.out.println("传递样式文件失败");
            }else {
                sldContentStr = URLDecoder.decode(sldFile.toString());

            }
            //判断workspace是否存在，若不存在则创建
            URL u = new URL(RESTURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, RESTUSER, RESTPW);
            GeoServerRESTPublisher publisher = manager.getPublisher();
            List<String> workspaces = manager.getReader().getWorkspaceNames();
            if (!workspaces.contains(ws))
            {
                boolean creatws = publisher.createWorkspace(ws);
                System.out.println("create ws:"+creatws);
            }else
            {
                System.out.println("workspace已经存在:"+ws);
            }

            //判断layer是否存在，若不存在则创建
            RESTLayer layer = manager.getReader().getLayer(ws,table_name);
            //判断工作区下是否已有此样式，没有增加,有则更新
            GeoServerRESTStyleManager styleManager = manager.getStyleManager();
            boolean b = styleManager.existsStyle(ws, sldName);
            String sldContent = sldContentStr;
            if(!b){
                boolean b1 = styleManager.publishStyleInWorkspace(ws, sldContent, sldName);
                if(!b1) {
                    System.out.println("新增样式失败");
                }else {
                    GSLayerEncoder layerEncoder = new GSLayerEncoder();
                    layerEncoder.setDefaultStyle(sldName);
                    //修改图层的样式文件
                    boolean publish =manager.getPublisher().configureLayer(ws,table_name,layerEncoder);
                }

            }else {
                String Content = styleManager.getSLD(ws, sldName);
                System.out.println(Content);

                boolean b2 = styleManager.updateStyleInWorkspace(ws, sldContent, sldName);

                GSLayerEncoder layerEncoder = new GSLayerEncoder();
                layerEncoder.setDefaultStyle(sldName);
                //修改图层的样式文件
                boolean publish =manager.getPublisher().configureLayer(ws,table_name,layerEncoder);
                //boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);
                if(!b2) {
                    System.out.println("更新样式失败");
                }
            }

        }catch (Exception mue){
            mue.printStackTrace();
        }

        return new ReturnValue(200,"图层已经发布成功");
    }
}
